package top.whysu.rongcloud.controller;

import io.rong.RongCloud;
import io.rong.models.Result;
import io.rong.models.chatroom.ChatroomMember;
import io.rong.models.chatroom.ChatroomModel;
import io.rong.models.response.ChatroomUserQueryResult;
import io.rong.models.response.CheckChatRoomUserResult;
import io.rong.models.response.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.whysu.rongcloud.api.ChatRoomBagResult;
import top.whysu.rongcloud.api.ChatRoomsResult;
import top.whysu.rongcloud.api.CheckChatRoomUsersResult;
import top.whysu.rongcloud.api.RongCloudSMS;
import top.whysu.rongcloud.api.dto.ChatroomMembers;
import top.whysu.rongcloud.constant.ConfigConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 聊天室
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-10-05 17:11:16
 */
@RestController
@RequestMapping("/rongCloud/chatroom")
public class ChatroomController {
    private RongCloud rongCloud = RongCloud.getInstance(ConfigConstant.appKey, ConfigConstant.appSecret);

    private RongCloudSMS rongCloudSMS = RongCloudSMS.getInstance(ConfigConstant.appKey, ConfigConstant.appSecret);

    //创造聊天室
    @GetMapping("/creates")
    Result create(@RequestParam("id") String[] id, @RequestParam("name") String[] name) {
        ChatroomModel[] chatroomModels = null;
        List<ChatroomModel> chatroomModelList = new ArrayList<>();
        for (int i = 0; i < id.length; i++) {
            ChatroomModel chatroomModel = new ChatroomModel();
            chatroomModel.setId(id[i]);
            chatroomModel.setName(name[i]);
            chatroomModelList.add(chatroomModel);
        }

        if (chatroomModelList != null && chatroomModelList.size() > 0) {
            chatroomModels = (ChatroomModel[]) chatroomModelList.toArray(new ChatroomModel[0]);
        }

        Result result = null;
        try {
            result = rongCloud.chatroom.create(chatroomModels);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //创造聊天室
    @GetMapping("/create")
    Result create_arrays(@RequestParam("id") String[] id, @RequestParam("name") String[] name) {
        ChatroomModel[] chatrooms = new ChatroomModel[id.length];
        for (int i = 0; i < id.length; i++) {
            ChatroomModel chatroomModel = new ChatroomModel();
            chatroomModel.setId(id[i]);
            chatroomModel.setName(name[i]);
            chatrooms[i] = chatroomModel;
        }

        Result result = null;
        try {
            result = rongCloud.chatroom.create(chatrooms);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //销毁聊天室
    @GetMapping("/destroy")
    Result destroy_chatroom(@RequestParam("chatroomId") String chatroomId) {
        ChatroomModel chatroomModel = new ChatroomModel();
        chatroomModel.setId(chatroomId);
        Result result = null;
        try {
            result = rongCloud.chatroom.destroy(chatroomModel);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //查询聊天室信息方法
    @GetMapping("/query")
    ChatRoomsResult query_chatroom(@RequestParam("chatroomId") String chatroomId) {
        ChatroomMembers chatroomMembers = new ChatroomMembers();
        chatroomMembers.setChatroomId(chatroomId);
        ChatRoomsResult result = null;
        try {
            result = rongCloudSMS.chatrooms.get(chatroomMembers);

        } catch (Exception e) {

            return new ChatRoomsResult("500", null);
        }

        return result;
    }

    //查询聊天室内用户方法
    @GetMapping("/user/query")
    Result user_query_chatroom(ChatroomModel chatroom) {

        Result result = null;
        try {
            result = rongCloud.chatroom.get(chatroom);

        } catch (Exception e) {

            return new ChatroomUserQueryResult(0, null);
        }

        return result;
    }

    //查询用户是否在聊天室方法
    @GetMapping("/user/exist")
    CheckChatRoomUserResult user_exist(ChatroomMember chatroomMember) {

        CheckChatRoomUserResult result = null;
        try {
            result = rongCloud.chatroom.isExist(chatroomMember);

        } catch (Exception e) {

            return new CheckChatRoomUserResult("500", false);
        }

        return result;
    }

    //批量查询用户是否在聊天室方法
    @GetMapping("/users/exist")
    CheckChatRoomUsersResult users_exist(ChatroomMembers chatroomMembers) {

        CheckChatRoomUsersResult result = null;
        try {
            result = rongCloudSMS.chatrooms.isExists(chatroomMembers);

        } catch (Exception e) {

            return new CheckChatRoomUsersResult("500", null);
        }

        return result;
    }

    /**
     * 聊天室成员禁言服务
     */
//添加禁言聊天室成员方法
    @GetMapping("/user/gag/add")
    Result gag_add(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.gag_add(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //移除禁言聊天室成员方法
    @GetMapping("/user/gag/rollback")
    Result gag_rollback(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.gag_rollback(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //查询被禁言聊天室成员方法
    @GetMapping("/user/gag/list")
    Result gag_list(@RequestParam("chatroomId") String chatroomId) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.gag_list(chatroomId);

        } catch (Exception e) {

            return new ChatRoomBagResult(500, "系统发生错误", null);
        }

        return result;
    }

    /**
     * 聊天室全局禁言
     */
    //添加聊天室全局禁言方法
    @GetMapping("/user/ban/add")
    Result ban_add(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.ban_add(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //移除聊天室全局禁言方法
    @GetMapping("/user/ban/remove")
    Result ban_remove(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.ban_remove(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //查询聊天室全局禁言用户方法
    @GetMapping("/user/ban/query")
    Result ban_query() {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.ban_query();

        } catch (Exception e) {

            return new ChatRoomBagResult(500, "系统发生错误", null);
        }

        return result;
    }


    /**
     * 聊天室封禁服务
     */
    //添加封禁聊天室成员方法
    @GetMapping("/user/block/add")
    Result block_add(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.block_add(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //移除封禁聊天室成员方法
    @GetMapping("/user/block/rollback")
    Result block_rollback(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.block_rollback(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //查询被封禁聊天室成员方法
    @GetMapping("/user/block/list")
    Result block_list(@RequestParam("chatroomId") String chatroomId) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.block_list(chatroomId);

        } catch (Exception e) {

            return new ChatRoomBagResult(500, "系统发生错误", null);
        }

        return result;
    }

    /**
     * 聊天室用户白名单服务
     */
    //添加聊天室白名单成员方法
    @GetMapping("/user/whitelist/add")
    Result whitelist_add(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.whitelist_add(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //移除聊天室白名单成员方法
    @GetMapping("/user/whitelist/remove")
    Result whitelist_remove(ChatroomMembers chatroomMembers) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.whitelist_remove(chatroomMembers);

        } catch (Exception e) {

            return new ResponseResult(500, null);
        }

        return result;
    }

    //查询聊天室白名单成员方法
    @GetMapping("/user/whitelist/query")
    Result whitelist_query(@RequestParam("chatroomId") String chatroomId) {

        Result result = null;
        try {
            result = rongCloudSMS.chatrooms.whitelist_query(chatroomId);

        } catch (Exception e) {

            return new ChatRoomBagResult(500, "系统发生错误", null);
        }

        return result;
    }

}
